package com.cheney.demo.config;

import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author chensi
 * @date 2021/11/15 16:51
 */
@Configuration
public class WxServerConfig {

    @Value("${wx.officialAccount.appId}")
    private String appId;
    @Value("${wx.officialAccount.appSecret}")
    private String appSecret;
    @Value("${wx.officialAccount.serverToken}")
    private String serverToken;
    @Value("${wx.officialAccount.serverAesKey}")
    private String serverAesKey;


    @Bean
    public WxMpDefaultConfigImpl wxMpDefaultConfigImpl() {
        WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();
        config.setAppId(appId); // 设置微信公众号的appid
        config.setSecret(appSecret); // 设置微信公众号的app corpSecret
        config.setToken(serverToken); // 设置微信公众号的token
        config.setAesKey(serverAesKey); // 设置微信公众号的EncodingAESKey
        return config;
    }

    @Bean
    public WxMpService wxMpService() {
        WxMpService wxMpService = new WxMpServiceImpl();// 实际项目中请注意要保持单例，不要在每次请求时构造实例，具体可以参考demo项目
        wxMpService.setWxMpConfigStorage(wxMpDefaultConfigImpl());
        return wxMpService;
    }
}
